#include "Former.h"


string Former::form(const std::string &str)
{
	string original = str;
	bool changed = false;

	//if(false == changed) changed = changed || form_time(original);
	//if(false == changed) changed = changed || form_money(original);
	//if(false == changed) changed = changed || form_number(original);
	if(false == changed) changed = changed || form_digit(original);

	return original;
}

//////////////////////

bool Former::form_time(std::string &str)
{
	if( str == "second" || str == "seconds" ||
		str == "minute" || str == "minutes" ||
		str == "hour" || str == "hours" ||
		str == "day" || str == "days" ||
		str == "week" || str == "weeks" ||
		str == "month" || str == "months" ||
		str == "year" || str == "years" ||
		str == "decade" || str == "decades" )
	{
		str = "#TIME#";
		return true;
	}

	return false;
}

bool Former::form_money(std::string &str)
{
	if( str == "dollar" || str == "dollars" ||
		str == "Cent" || str == "Cents" )
	{
		str = "#MONEY#";
		return true;
	}

	return false;
}

bool Former::form_number(std::string &str)
{
	if( str == "one" || 
		str == "two" ||
		str == "three" ||
		str == "four" ||
		str == "five" ||
		str == "six" ||
		str == "seven" ||
		str == "eight" ||
		str == "nine" ||
		str == "ten" ||
		str == "hundred" ||
		str == "II" || str == "III" )
	{
		str = "#NUM#";
		return true;
	}

	return false;
}

bool Former::form_digit(std::string &str)
{
	bool is_pre_digit = false, rst = false;
	string tmp = "";

	for(int i=0; i<(int)str.length(); i++)
	{
		if(fs_algo::is_digit(str[i]))
		{
			rst = true;
			if(is_pre_digit) 
			{
				tmp += "D";
			}
			else 
			{
				tmp += "*D";
				is_pre_digit = true;
			}
		}
		else
		{
			if(is_pre_digit) 
			{
				tmp += "*";
				is_pre_digit = false;
			}
			tmp += str[i];
		}
	}

	if(is_pre_digit) 
	{
		tmp += "*";
	}

	if(rst) str = tmp;
	return rst;
}